package com.grocery.controller;

import com.grocery.bean.Order;
import com.grocery.service.impl.OrderService;
import com.grocery.util.LayData;
import com.grocery.util.Page;
import com.grocery.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @RequestMapping("add")
    @ResponseBody
    public LayData add(@RequestParam("id[]") List<Integer> id, @RequestParam("number[]") List<Integer> number,Integer addressid,Integer userid){
        //获取userid
        int orderid=orderService.addOrder(id,number,addressid,userid);
        return LayData.success(200,"订单创建成功",orderid);
    }

    @RequestMapping("payment")
    @ResponseBody
    public LayData payment(Integer id){
        String code=orderService.payment(id);
        return LayData.success(200,"",code);
    }

    @RequestMapping("paymentsuccess")
    @ResponseBody
    public LayData paymentSuccess(@RequestParam HashMap map){
        orderService.paymentSuccess(map);
        return LayData.success();
    }

    @RequestMapping("getorderstate")
    @ResponseBody
    public LayData getorderstate(Integer id){
        Order order=orderService.getOrderByID(id);
        return LayData.success(200,"",order.getState());
    }

    @RequestMapping("getallorder")
    @ResponseBody
    public LayData getallorder(Page page, String text){
        List<Order> list = orderService.getAllOrder();
        int count=orderService.getCount(text);
        return new LayData(list,count);
    }

    @RequestMapping("editstate")
    @ResponseBody
    public int editState(Integer code,Integer state){
        return orderService.editState(code,state);
    }

    @RequestMapping("getorderbyuserid")
    @ResponseBody
    public List<Order> getorderbyuserid(Integer userid){
        return orderService.getOrderByuserID(userid);
    }
}
